﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Threading;

using EnvironmentalMonitor.Support.Instruction.In.Request;
using EnvironmentalMonitor.Support.Instruction.In.Response;
using EnvironmentalMonitor.Support.Instruction.Out.Request;
using EnvironmentalMonitor.Support.Instruction.Out.Response;
using EnvironmentalMonitor.Support.Instruction;
using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.WebApplication.Abstract
{
    /// <summary>
    /// 抽象设置保存Json数据页面类
    /// </summary>
    public abstract class AbstractSetupSaveJson : EnvironmentalMonitor.WebApplication.Abstract.AbstractSaveJson
    {
        /// <summary>
        /// 查询任务
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="successType"></param>
        /// <param name="errorType"></param>
        /// <param name="now"></param>
        /// <returns></returns>
        public Nullable<bool> QueryTask(string ip, string successType, string errorType, DateTime now)
        {
            Nullable<bool> result = null;

            Thread.Sleep(Variable.Timeout * 1000);

            if (AbstractInstruction.InstructionTasks != null)
            {
                for (int i = AbstractInstruction.InstructionTasks.Count - 1; i >= 0; i--)
                {
                    bool remove = false;
                    if (now > AbstractInstruction.InstructionTasks[i].Refresh)
                    {
                        DateTime before = now.AddMinutes(-30);
                        if (before > AbstractInstruction.InstructionTasks[i].Refresh)
                        {
                            remove = true;
                        }
                    }
                    else
                    {
                        if ((AbstractInstruction.InstructionTasks[i].Ip == ip) && (AbstractInstruction.InstructionTasks[i].Type == successType) && (AbstractInstruction.InstructionTasks[i].Done))
                        {
                            result = true;
                            remove = true;
                        }
                        if ((AbstractInstruction.InstructionTasks[i].Ip == ip) && (AbstractInstruction.InstructionTasks[i].Type == errorType) && (AbstractInstruction.InstructionTasks[i].Done))
                        {
                            result = false;
                            remove = true;
                        }
                    }
                    if (remove)
                    {
                        AbstractInstruction.InstructionTasks.RemoveAt(i);
                    }
                }
            }

            return result;
        }
    }
}